package com.lin.controller.back;

import com.github.pagehelper.PageInfo;
import com.lin.entity.Article;
import com.lin.entity.Comment;
import com.lin.entity.ResponseData;
import com.lin.service.ArticleService;
import com.lin.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lin
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private ArticleService articleService;

    @RequestMapping("")
    public String admin(HttpServletRequest request) {
        //准备所需数据
        // 1 关于文章所需的数据
        //所有文章数据
        Integer totalArticlesNum = articleService.getTotalArticlesNum();
        //最新发布的文章
        List<Article> recentArticle = articleService.findRecentArticle(5);

        //2 关于 文章评论 的所需数据
        //所有评论数据
        Integer totalCommentsNum = commentService.getTotalCommentsNum();
        //最新发布的 文章评论
        List<Comment> recentComments = commentService.findRecentComments(5);

        request.setAttribute("articleNum", totalArticlesNum);
        request.setAttribute("commentNum", totalCommentsNum);
        request.setAttribute("articles", recentArticle);
        request.setAttribute("comments", recentComments);
        /* request.setAttribute("commons",new Commons());*/
        return "back/index";
    }

    @RequestMapping("/article/toEditPage")
    public String toEditPage(HttpServletRequest request) {
        return "back/article_edit";
    }

    /**
     * 发布文章
     *
     * @param article
     * @return
     */
    @RequestMapping("/article/publish")
    @ResponseBody
    public ResponseData publish(Article article) {
        //添加到文章表格
        articleService.pubishArticle(article);
        return ResponseData.OK();
    }

    /**
     * 文章列表(页面跳转)
     *
     * @param request
     * @param page
     * @param count
     * @return
     */
    @RequestMapping("/article")
    public String article(HttpServletRequest request, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "count", defaultValue = "10") Integer count) {
        //文章分页查询
        PageInfo<Article> articles = articleService.findArticleWithPage(page, count);
        request.setAttribute("articles", articles);
        //跳转到文章列表
        return "back/article_list";
    }

    /**
     * 后台管理--文章编辑
     *
     * @param request
     * @param articleId
     * @return
     */
    @RequestMapping("/article/{articleId}")
    public String toEditPage(HttpServletRequest request, @PathVariable("articleId") Integer articleId) {
        //获取文章数据,再进行跳转
        Article byId = articleService.findById(articleId);
        request.setAttribute("contents", byId);
        return "back/article_edit";
    }

    /**
     * 处理 编辑请求
     *
     * @param article
     * @return
     */
    @RequestMapping("/article/modify")
    @ResponseBody
    public ResponseData modify(Article article) {
        //修改t_article数据
        articleService.updateArticle(article);
        return ResponseData.OK();
    }

    /**
     * 处理 删除文章 请求
     *
     * @param articleId
     * @return
     */
    @RequestMapping("/article/delect/{articleId}")
    public String delectArticle(@PathVariable("articleId") Integer articleId) {
        articleService.deleteArticle(articleId);
        return "forward:/admin/article";
    }


}
